package com.example.wifi_scan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * 这个Activity是选择WiFi界面内，可以选择三个WiFi来进行采集
 */
public class SecondActivity extends Activity {

    ArrayList<String> selected = new ArrayList<>();
    String[] data;

    private TextView selectedItem;
    private ListView listView;
    private Button okButton, cancel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectwifi);

        final Intent intent = getIntent();
        String wifidata = intent.getStringExtra("wifidata");
        data = resource.getData(wifidata);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                SecondActivity.this, android.R.layout.simple_list_item_1, data
        );

        selectedItem = (TextView) findViewById(R.id.selectedItem);
        listView = (ListView) findViewById(R.id.list_view);
        okButton = (Button) findViewById(R.id.ok);
        cancel = (Button) findViewById(R.id.cancel);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String string = data[position];

                if (selected.contains(string)) {
                    selected.remove(string);
                } else {
                    if (selected.size() >= 3) {
                        Toast.makeText(SecondActivity.this, "已经选了3个了", Toast.LENGTH_SHORT).show();
                    } else {
                        selected.add(string);
                    }
                }
                selectedItem.setText(resource.arraytostring(selected));
            }
        });
        listView.setAdapter(adapter);

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selected.size() < 3) {
                    Toast.makeText(SecondActivity.this, "选的还不够3个，再选几个把", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent1 = new Intent();
                    intent1.putExtra("data_return", resource.dataForIntent(selected));
                    setResult(RESULT_OK, intent1);
                    finish();
                }

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent();
                setResult(RESULT_CANCELED, intent1);
                finish();
            }
        });
    }
}
